using System.Collections;
using QFramework.Framework;
using QFramework.Framework.Util;
using UnityEditor;
using UnityEngine;
#if UNITY_EDITOR
#endif

namespace QFramework.Example._2.框架示例
{
    /**
     * 框架-示例
     */
    public class FrameworkExample : MonoBehaviourSimplify
    {
        private void Awake()
        {
            RegisterMsg("Do", DoSomething);
            RegisterMsg("Do", DoSomething);
            RegisterMsg("Do1", _ => { });
            RegisterMsg("Do2", _ => { });
            RegisterMsg("Do3", _ => { });

            RegisterMsg("OK", data =>
            {
                Debug.Log(data);
                UnRegisterMsg("OK");
            });
        }

        private IEnumerator Start()
        {
            SendMsg("OK", "hello");
            SendMsg("OK", "hello");

            MsgDispatcher.Send("Do", "hello");

            yield return new WaitForSeconds(1.0f);

            MsgDispatcher.Send("Do", "hello1");
        }
#if UNITY_EDITOR
        [MenuItem("QFramework/Example/2.框架示例", false, 2)]
        private static void MenuClicked()
        {
            MsgDispatcher.UnRegisterAll("Do");
            MsgDispatcher.UnRegisterAll("OK");

            EditorApplication.isPlaying = true;
            new GameObject("MsgReceiverObj").AddComponent<FrameworkExample>();
        }
#endif

        protected override void OnBeforeDestroy()
        {
        }

        private void DoSomething(object data)
        {
            Debug.LogFormat("Received Do msg:{0}", data);
        }
    }
}